package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.anno.Log;
import com.itheima.aop.OperateLogAspect;
import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.OperateLogMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.*;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private OperateLogMapper operateLogMapper;
    @Autowired
    private OperateLogAspect operateLogAspect;

    //统计员工职位人数
    @Override
    public JobOption getJobData() {
        //1.调用Mapper接口获取统计数据
        List<Map<String, Object>> list = empMapper.countEmpJobData();//pos=教研主管，num=1
        //2.组装结果并返回
        List<Object> jobList = list.stream().map(dataMap -> dataMap.get("pos")).toList();
        List<Object> dataList = list.stream().map(dataMap -> dataMap.get("num")).toList();
        return new JobOption(jobList,dataList);
    }

    //统计员工性别人数
    @Override
    public List<Map<String, Object>> getEmpGender() {
        return empMapper.countEmpGender();
    }

    //统计学员学历
    @Override
    public List<Map<String, Object>> getStudentDegreeData() {
        return studentMapper.countStuDegree();
    }


    //班级人数统计
    @Override
    public StuCount getStuCount() {
        //1.调用Mapper接口获取统计数据
        List<Map<String, Object>> list = studentMapper.stuCount();
        //2.组装结果并返回
        List<Object> clazzList = list.stream().map(dataMap -> dataMap.get("name")).toList();
        List<Object> dataList = list.stream().map(dataMap -> dataMap.get("studentCount")).toList();
        return new StuCount(clazzList,dataList);
    }

    //日志查询
    @Override
    public PageResult<OperateLog> page(Integer page, Integer pageSize) {
        //1.设置分页参数(PageHelper)
        PageHelper.startPage(page,pageSize);
        //2.执行查询
        List<OperateLog> operateLogs = operateLogMapper.list();
        //3.解析查询结果并封装
        Page<OperateLog> p = (Page<OperateLog>) operateLogs;
        return new PageResult<>(p.getTotal(), p.getResult());
    }
}
